<html>
<head><meta charset="utf-8"><title>get all variables in scope and check for shadowing · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20all.20variables.20in.20scope.20and.20check.20for.20shadowing.html">get all variables in scope and check for shadowing</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="236494421"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20all%20variables%20in%20scope%20and%20check%20for%20shadowing/near/236494421" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jakub Kądziołka <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20all.20variables.20in.20scope.20and.20check.20for.20shadowing.html#236494421">(Apr 28 2021 at 12:04)</a>:</h4>
<p>Inspired by <a href="https://stackoverflow.com/questions/67296177/rust-book-listing-10-15when-i-use-let-largest-item-the-largest-cant-be">this SO post</a>, I would like to implement the following logic: if a <code>let</code> binding is unused, and another variable with the same name exists, suggest removing the <code>let</code>.</p>
<p>I've found the relevant code in <code>rustc_passes/src/liveness.rs</code> -&gt; <code>fn report_unused</code>. I can get a hir <code>Node::Local</code> with</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">hir</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">ir</span><span class="p">.</span><span class="n">tcx</span><span class="p">.</span><span class="n">hir</span><span class="p">();</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">parent</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hir</span><span class="p">.</span><span class="n">get_parent_node</span><span class="p">(</span><span class="n">first_hir_id</span><span class="p">);</span><span class="w"></span>
<span class="fm">dbg!</span><span class="p">(</span><span class="n">hir</span><span class="p">.</span><span class="n">get</span><span class="p">(</span><span class="n">parent</span><span class="p">));</span><span class="w"></span>
</code></pre></div>
<p>However, I can't find how to check if this variable shadows some other variable. I tried finding how other code does that, but I only found the maze that is <code>rustc_resolve</code>. I'd appreciate any hints.</p>
<p>I'd also like to know how to get the span of the <code>let</code>, but that's a secondary concern for now.</p>



<a name="236497411"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20all%20variables%20in%20scope%20and%20check%20for%20shadowing/near/236497411" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20all.20variables.20in.20scope.20and.20check.20for.20shadowing.html#236497411">(Apr 28 2021 at 12:27)</a>:</h4>
<p>This will be tricky because you need access to both control flow and scoping info, and the resolver is consumed when creating the TyCtxt. <span class="user-mention" data-user-id="123856">@Vadim Petrochenkov</span> may have ideas.</p>



<a name="236547105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20all%20variables%20in%20scope%20and%20check%20for%20shadowing/near/236547105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jakub Kądziołka <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20all.20variables.20in.20scope.20and.20check.20for.20shadowing.html#236547105">(Apr 28 2021 at 17:30)</a>:</h4>
<p>I've created an issue to track this improvement: <a href="https://github.com/rust-lang/rust/issues/84664">https://github.com/rust-lang/rust/issues/84664</a></p>



<a name="236752570"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20all%20variables%20in%20scope%20and%20check%20for%20shadowing/near/236752570" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jakub Kądziołka <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20all.20variables.20in.20scope.20and.20check.20for.20shadowing.html#236752570">(Apr 29 2021 at 21:46)</a>:</h4>
<p><span class="user-mention" data-user-id="123856">@Vadim Petrochenkov</span> Hi, did you have a chance to look at this? Just making sure Zulip didn't eat the notification or something ;)</p>



<a name="236755598"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20all%20variables%20in%20scope%20and%20check%20for%20shadowing/near/236755598" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vadim Petrochenkov <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20all.20variables.20in.20scope.20and.20check.20for.20shadowing.html#236755598">(Apr 29 2021 at 22:14)</a>:</h4>
<p>It's possible collect some relevant data during name resolution, then pass it through <code>Resolver</code> -&gt; <code>ResolverOutputs</code> -&gt; <code>GlobalCtxt</code> and use it during liveness analysis, but I don't think it's something that's reasonable to do for a niche diagnostic.</p>



<a name="236793021"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20all%20variables%20in%20scope%20and%20check%20for%20shadowing/near/236793021" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20all.20variables.20in.20scope.20and.20check.20for.20shadowing.html#236793021">(Apr 30 2021 at 06:51)</a>:</h4>
<p>This is only a valid suggestion if the initial <code>let</code> is mutable and <em>permits</em> assignment, which none of the examples presented so far actually allow. The compiler is allowed to make questionable suggestions, I would think, but it should not make overtly erroneous ones.</p>



<a name="236793097"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20all%20variables%20in%20scope%20and%20check%20for%20shadowing/near/236793097" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20all.20variables.20in.20scope.20and.20check.20for.20shadowing.html#236793097">(Apr 30 2021 at 06:52)</a>:</h4>
<p>Ehhh, it's probably still closer than the original code. Suggestions don't need to be 100% valid to be helpful</p>



<a name="236793540"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20all%20variables%20in%20scope%20and%20check%20for%20shadowing/near/236793540" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20all.20variables.20in.20scope.20and.20check.20for.20shadowing.html#236793540">(Apr 30 2021 at 06:59)</a>:</h4>
<p>True, I think it can mostly be solved by couching the phrasing differently: instead of outright saying, something like asking "did you mean to assign instead?" would minimize coming off like the Rust compiler explicitly said you should assign to an immutable variable.</p>



<a name="236798782"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20all%20variables%20in%20scope%20and%20check%20for%20shadowing/near/236798782" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jakub Kądziołka <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20all.20variables.20in.20scope.20and.20check.20for.20shadowing.html#236798782">(Apr 30 2021 at 07:54)</a>:</h4>
<p>we could emit a "you will need to change this to <code>let mut</code>" hint if you insist</p>



<a name="236798810"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/get%20all%20variables%20in%20scope%20and%20check%20for%20shadowing/near/236798810" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jakub Kądziołka <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/get.20all.20variables.20in.20scope.20and.20check.20for.20shadowing.html#236798810">(Apr 30 2021 at 07:55)</a>:</h4>
<p>but even without that it will send you down the right path, as that hint will appear on the next compile</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>